package com.gm.blog.interceptor;

import com.gm.blog.enums.ResultEnum;
import com.gm.blog.exception.BlogException;
import com.gm.blog.pojo.Admin;
import com.gm.blog.utils.ShiroUtils;
import com.gm.blog.utils.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取token
        String token = request.getHeader("Authorization");
        if(StringUtils.isNotBlank(token)){
            //token不为空，获取当前登录用户
            Admin loginAdmin= ShiroUtils.getLoginAdmin();
            if(loginAdmin!=null){
                //说明token有效
                return true;
            }
        }
        throw new BlogException(ResultEnum.NOT_LOGIN);
    }
}
